const state = {
	systemBarHeight: null
}

const mutations = {
	SET_SYSTEM_BAR_HEIGHT(state, systemBarHeight) {
		state.systemBarHeight = systemBarHeight
	}
}

const actions = {
	// 定义异步获取系统栏高度的action
	async getSystemBarHeight({
		commit
	}) {
		const that = this;
		return new Promise((resolve, reject) => {
			uni.getSystemInfo({
				success(res) {
					// 判断当前所在的端，这里以常见的微信小程序、H5、APP端为例
					if (res.uniPlatform === 'devtools') {
						// 在开发工具中，可根据实际情况设置一个默认值或模拟值
						that.systemBarHeight = 90;
					} else if (res.uniPlatform === 'mp-weixin') {
						// 微信小程序端
						that.systemBarHeight = 4 + res.statusBarHeight + (uni
							.getMenuButtonBoundingClientRect().height + (
								uni.getMenuButtonBoundingClientRect().top - res.statusBarHeight) * 2);
					} else if (res.uniPlatform === 'h5') {
						// H5端，可根据浏览器的默认样式等因素设置合适的值
						that.systemBarHeight = 10;
					} else if (res.uniPlatform === 'app-plus') {
						// APP端，可根据APP的设计需求设置值
						that.systemBarHeight = res.statusBarHeight;
					} else if (res.uniPlatform === 'web') {
						// APP端，可根据APP的设计需求设置值
						that.systemBarHeight = 50;
					}
					// 根据计算出的systemBarHeight设置yunContentStyle，这里暂不处理yunContentStyle相关逻辑，主要关注高度值更新到state
					commit('SET_SYSTEM_BAR_HEIGHT', that.systemBarHeight);
					resolve(that.systemBarHeight);
				},
				fail(err) {
					reject(err);
				}
			});
		});
	}
}

const getters = {
	systemBarHeight: state => state.systemBarHeight
}

export default {
	namespaced: true,
	state,
	mutations,
	actions,
	getters
}